package com.treasure.mall.biz.service.core.wechat.impl;

import com.treasure.mall.biz.base.AbstractSearchableCrudService;
import com.treasure.mall.biz.base.SpecificationAndSort;
import com.treasure.mall.biz.dto.BaseSaveDTO;
import com.treasure.mall.biz.dto.BaseSearchDTO;
import com.treasure.mall.biz.dto.condition.BaseSearchCondition;
import com.treasure.mall.biz.dto.wechat.KeywordMessageDTO;
import com.treasure.mall.biz.entity.wechat.WechatKeywordMessagePO;
import com.treasure.mall.biz.entity.wechat.WechatKeywordMessagePO_;
import com.treasure.mall.biz.mapstruct.WxMapStruct;
import com.treasure.mall.biz.repository.wechat.WechatKeywordMessageRepository;
import com.treasure.mall.biz.service.core.wechat.WechatKeywordMessageService;
import com.treasure.mall.zbq.common.exception.ApiException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * <p>
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.biz.service.core.wechat.impl
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/9/15 11:10 上午
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 *
 */
@Service
public class WechatKeywordMessageServiceImpl extends AbstractSearchableCrudService<WechatKeywordMessagePO, Long> implements WechatKeywordMessageService {

    private WechatKeywordMessageRepository messageRepository;

    protected WechatKeywordMessageServiceImpl(WechatKeywordMessageRepository repository) {
        super(repository);
        messageRepository = repository;
    }

    @Override
    protected SpecificationAndSort<WechatKeywordMessagePO> specificationAndSort(BaseSearchDTO searchDTO) {
        BaseSearchCondition condition = (BaseSearchCondition) searchDTO;
        Specification<WechatKeywordMessagePO> specification = (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            predicates.add(cb.equal(root.get(WechatKeywordMessagePO_.deleted), false));
            if (StringUtils.isNotBlank(condition.getKeyword())) {
                predicates.add(cb.like(root.get(WechatKeywordMessagePO_.keyword), likeAll(condition.getKeyword())));
            }
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        Sort sort = new Sort(Sort.Direction.DESC, "Id");
        return new SpecificationAndSort<>(specification, sort);
    }

    @Override
    public WechatKeywordMessagePO findByKeyword(String keyword) {
        return messageRepository.findFirstByKeywordOrderByIdDesc(keyword);
    }

    @Override
    public WechatKeywordMessagePO save(BaseSaveDTO saveDTO) {
        KeywordMessageDTO dto = (KeywordMessageDTO) saveDTO;
        if (dto == null) {
            throw new ApiException("参数错误");
        }
        WechatKeywordMessagePO po = WxMapStruct.INSTANCE.dto2po(dto);
        return save(po);
    }
}
